package com.opages.netty.demo.netty.rpc.common;

import io.netty.util.CharsetUtil;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author daizhong.liu
 * @create 2021-12-15 18:01
 **/
public class RpcProtocol {
    public static final String HEADER_PROTOCOL = "OPAGES:";
    private int[] protocolLen = new int[5];
    private byte[] header;
    private byte[] className;
    private byte[] methodName;
    private byte[] params;
    private byte[] result;

    public RpcInvoker toInvoker() {
        try {
            RpcInvoker invoker = new RpcInvoker();
            String clazzStr = new String(className, CharsetUtil.UTF_8);
            String methodStr = new String(methodName, CharsetUtil.UTF_8);
            if(clazzStr.indexOf("Object") == -1) {
                clazzStr = clazzStr+"Impl";
            }
            Class clazz = Class.forName(clazzStr);
            Method method = clazz.getDeclaredMethod(methodStr, null);
            invoker.setMethod(method);
            if (params != null) {
                String param = new String(params, CharsetUtil.UTF_8);
                System.err.println("存在参数->"+param);
            }
            if(result != null && result.length > 0) {
                String r = new String(result,CharsetUtil.UTF_8);
                invoker.setResult(r);
            }
            return invoker;
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }

    public RpcProtocol(){
        header = HEADER_PROTOCOL.getBytes();
        protocolLen[0] = header.length;
    }

    public int[] getProtocolLen() {
        return protocolLen;
    }

    public void setProtocolLen(int[] protocolLen) {
        this.protocolLen = protocolLen;
    }

    public byte[] getHeader() {
        return header;
    }

    public void setHeader(byte[] header) {
        this.header = header;
        protocolLen[0] = this.header.length;
    }

    public byte[] getClassName() {
        return className;
    }

    public void setClassName(byte[] className) {
        this.className = className;
        protocolLen[1] = this.className.length;
    }

    public byte[] getMethodName() {
        return methodName;
    }

    public void setMethodName(byte[] methodName) {
        this.methodName = methodName;
        protocolLen[2] = this.methodName.length;
    }

    public byte[] getParams() {
        return params;
    }

    public void setParams(byte[] params) {
        this.params = params;
        protocolLen[3] = this.params.length;
    }

    public byte[] getResult() {
        return result;
    }

    public void setResult(byte[] result) {
        this.result = result;
        protocolLen[4] = this.result.length;
    }

    @Override
    public String toString() {
        StringBuilder r = new StringBuilder();
        r.append("RpcProtocol{")
                .append("protocolLen=" + Arrays.toString(protocolLen))
                .append(", header=" + new String(header, CharsetUtil.UTF_8))
                .append(", className=" + new String(className, CharsetUtil.UTF_8))
                .append(", methodName=" + new String(methodName, CharsetUtil.UTF_8));
        if(params != null) {
            r.append(", params=" + new String(params, CharsetUtil.UTF_8));
        }
        if(result != null) {
            r.append(", result=" + new String(result, CharsetUtil.UTF_8));
        }
        r.append('}');
        return r.toString();
    }
}
